<?php

namespace App\Http\Requests;

use App\Http\Constants\ClosingRuleConstants;
use App\Http\Constants\VerificationModeConstants;
use App\Models\CoinsApplication;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class CoinsApplicationCreateRequest extends FormRequest
{
    use CommonRequest;

    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'name' => [
                'required',
                Rule::unique((new CoinsApplication())->getTable(),'name')
            ],
            'verify_type' => [
                'required',
                Rule::in(array_column(VerificationModeConstants::VERIFICATION_MODE,'value'))
            ],
            'verify_url' => 'url',
            'settle_rule_id' => [
                'required',
                Rule::in(array_column(ClosingRuleConstants::CLOSING_RULE,'value'))
            ],
            'description' => 'required',
        ];
    }

    public function messages()
    {
        $verify_type_in_msg = implode('or',array_column(VerificationModeConstants::VERIFICATION_MODE,'label'));
        $settle_rule_id_in_msg = implode('or',array_column(ClosingRuleConstants::CLOSING_RULE,'label'));

        return [
            'name.required' => '名称不能为空',
            'name.unique' => '应用名称已存在',
            'verify_type.required' => '验证不能为空',
            'verify_type.in' => '验证模式只能是:'.$verify_type_in_msg,
            'verify_url.url' => '自定义验证路径格式错误',
            'settle_rule_id.required' => '结算规则不能为空',
            'settle_rule_id.in' => '结算规则只能是:'.$settle_rule_id_in_msg,
            'description.required' => '描述不能为空',
        ];
    }
}
